<?php
// $query_string = $_SERVER["QUERY_STRING"];
// keyword: 关键字
// start: 起始
// count: 结果数目
// category：资源分类 video|image|document|music|package|software
$page = empty($_GET['page']) ? 1 : $_GET['page'];
$keyword = $_GET['keyword']; //如果为空可以考虑返回一个message
$count = empty($_GET['count']) ? 30 : $_GET['count'];
$start = $count * ($page - 1);
$query_string = 'keyword='.urlencode($keyword).'&count='.$count.'&start='.$start;
if(!empty($_GET['category'])) $query_string .= '&category='.$_GET['category'];

// echo $query_string;
$api_address = 'https://api.kuaisohezi.com/search?'.$query_string;
// echo $api_address;
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => $api_address,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_POSTFIELDS => "",
  CURLOPT_SSL_VERIFYPEER => false,
  CURLOPT_SSL_VERIFYHOST => false,
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}